package com.heima.behavior.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.behavior.mapper.ApUnLikesBehaviorMapper;
import com.heima.behavior.service.ApBehaviorService;
import com.heima.behavior.service.ApUnLikesBehaviorService;
import com.heima.model.behavior.dto.UnLikesBehaviorDto;
import com.heima.model.behavior.pojo.ApBehaviorEntry;
import com.heima.model.behavior.pojo.ApUnlikesBehavior;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.threadlocal.ApUserThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@Transactional
public class ApUnlikesBehaviorServiceImpl extends ServiceImpl<ApUnLikesBehaviorMapper, ApUnlikesBehavior> implements ApUnLikesBehaviorService {


    @Autowired
    ApBehaviorService apBehaviorService;
    /**
     * APP点赞行为
     * @param dto
     * @return
     */
    @Override
    public ResponseResult unlike(UnLikesBehaviorDto dto) {
        //检查参数
        if (dto==null||dto.getArticleId()==null||dto.getType()==null){
            ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        ApUser apUser = ApUserThreadLocalUtils.getUser();
        if (dto.getEquipmentId()==null){
            if (apUser==null){
                ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
            }else {
                if (apUser.getId()==0){ //0表示游客登录
                    dto.setEquipmentId(Long.valueOf(apUser.getPhone()));
                }else {
                    dto.setEquipmentId(apUser.getId().longValue());
                }
            }
        }
        //查询数据库
        ApBehaviorEntry apBehaviorEntry = apBehaviorService.getOne(Wrappers.<ApBehaviorEntry>lambdaQuery().eq(ApBehaviorEntry::getEntryId, dto.getEquipmentId()));
        if (apBehaviorEntry==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
           /*  apBehaviorEntry=new ApBehaviorEntry();
            apBehaviorEntry.setEntryId(dto.getEquipmentId().intValue());
            apBehaviorEntry.setType(dto.getType());
            apBehaviorEntry.setCreatedTime(new Date());
            apBehaviorService.save(apBehaviorEntry);*/
        }
        ApUnlikesBehavior unlikesBehavior = getOne(Wrappers.<ApUnlikesBehavior>lambdaQuery().eq(ApUnlikesBehavior::getArticleId, dto.getArticleId()).eq(ApUnlikesBehavior::getEntryId, dto.getEquipmentId()));

        if (unlikesBehavior==null){
            unlikesBehavior=new ApUnlikesBehavior();
            unlikesBehavior.setArticleId(dto.getArticleId());
            unlikesBehavior.setCreatedTime(new Date());
            unlikesBehavior.setEntryId(dto.getEquipmentId().intValue());
        }
        unlikesBehavior.setType(dto.getType());
        //保存数据
        saveOrUpdate(unlikesBehavior);
        return ResponseResult.errorResult(AppHttpCodeEnum.SUCCESS);
    }

}
